package com.example.pandaso.controller;

import com.example.pandaso.pojo.*;
import com.example.pandaso.service.BlockService;
import com.example.pandaso.service.impl.UserService;
import com.example.pandaso.utils.JwtUtils;
import com.example.pandaso.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * (Block)表控制层
 *
 * @since 2023-06-04 19:56:24
 */
@RestController
@RequestMapping("/api")
public class BlockController {
    /**
     * 服务对象
     */
    @Resource
    private BlockService blockService;

    @Autowired
    private UserService userService;

    @Autowired
    private JwtUtils jwtUtils;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/block/{id}")
    public ResponseEntity<Block> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.blockService.queryById(id));
    }

    /*
     * 通过useid查询block数据
     * 默认uerid=0
     */
    @GetMapping("/block/getUserTag/{userid}")
    public ResponseEntity<List<Object>> queryByUserid(@PathVariable("userid") Integer id) {
        List<Object> tagsVoList = this.blockService.queryByUserid(id);

        if (tagsVoList != null)
            return ResponseEntity.ok(tagsVoList);
        else
            return (ResponseEntity<List<Object>>) ResponseUtils.error(HttpStatus.NOT_FOUND, "用户数据不存在");
    }

    /**
     * 新增数据
     *
     * @param tagsVo 实体
     * @return 新增结果
     */
    @PostMapping("/block/inserter/{userid}")
    public ResponseEntity<?> add(@PathVariable("userid") Integer id, @RequestBody TagsVo tagsVo) {
        List<Tags> tags = tagsVo.getTags();

        String tagName = null;
        String tagUrl = null;
        for (Tags tag : tags) {
            tagName = tag.getTag_Name();
            tagUrl = tag.getTag_URL();
        }
        boolean status = this.blockService.insert(tagsVo.getBlock_name(),
                tagName, tagUrl, id);

        return ResponseEntity.ok(status);
    }

    @GetMapping("/block/getUserId")
    public ResponseEntity<?> getUserId(@RequestHeader String token) {
        // 解析JWT令牌，获取用户名
        int userId;
        String username = jwtUtils.getUsernameFromToken(token);
        // 验证token
        boolean isoverdue = jwtUtils.validateToken(token, username);
        if (!isoverdue) {
            return ResponseUtils.error(HttpStatus.UNAUTHORIZED, "token过期");
        } else {
            // 根据用户名，查找用户id
            userId = userService.findUserIdByuserName(username);
        }
        return ResponseUtils.ok(userId);
    }

    /**
     * 编辑数据
     *
     * @param block 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Block> edit(Block block) {
        return ResponseEntity.ok(this.blockService.update(block));
    }

    /**
     * 删除数据
     *
     * @param tagId 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/block/deleteTagByTagId")
    public ResponseEntity<Boolean> deleteById(@RequestParam("tag_id") Integer tagId) {
        System.out.println(tagId);
        return ResponseEntity.ok(this.blockService.deleteById(tagId));
    }
}
